package Java7_Concurrency_Cookbook.fork_join.recursive_task;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * DocumentTask 中 invokeAll() 如果符合条件, 会递归调用 compute()
 * @author lixiangke
 * @date 2017/11/22
 */
public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        DocumentMock mock = new DocumentMock();
        String[][] document = mock.generateDocument(100, 1000, "the");
        DocumentTask task = new DocumentTask(document, 0, 100, "the");
        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(task);
//        do {
//            System.out.printf("**********************\n");
//            System.out.printf("Main: Parallelism: %d\n", pool.getParallelism());
//            System.out.printf("Main: Active Threads: %d\n", pool.getActiveThreadCount());
//            System.out.printf("Main: Zoom Count: %d\n", pool.getQueuedTaskCount());
//            System.out.printf("Main: Steal Count    : %d\n", pool.getStealCount());
//            System.out.printf("**********************\n");
//            TimeUnit.MINUTES.sleep(1);
//        } while (!task.isDone());

        pool.shutdown();
        pool.awaitTermination(1, TimeUnit.DAYS);

        System.out.printf("Main: The word appears %d in the document", task.get());
    }
}
